<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request; 
use Illuminate\Support\Facades\DB;

Class TongjiController extends AdminController{
    
    /**
     * 推广团队数据统计
     * @param Request $request
     */
    public function index(Request $request){
        
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Methods:POST,GET");
        header("Access-Control-Allow-Headers:x-requested-with,content-type");
        header("Content-type:text/json;charset=utf-8");
        
        $data = array(
            'Total'=>0,
            'Today'=>0,
            'TeamTotal'=>array(
                'team1'=>0,
                'team2'=>0,
                'team3'=>0,
            ),
            'TeamToday'=>array(
                'team1'=>0,
                'team2'=>0,
                'team3'=>0,
            ),
        );
        $today = " and login_at > '".date('Y-m-d')."' ";
        $commonSql = " login_client!='' ";
        $sqlTotal = "select count(*) as c from mega_member_channel where $commonSql";
        $sqlTotalCount = DB::select($sqlTotal);
        $data['Total'] = $sqlTotalCount[0]->c  + 36; ///总人数
        
        $sqlTodayCount = DB::select($sqlTotal.$today);
        $data['Today'] = $sqlTodayCount[0]->c; ///今日总人数
        
        $sqlListAll = "select channel_code from mega_member_channel where $commonSql";
        $ListAll = DB::select($sqlListAll);
        foreach ($ListAll as $line){
            $code = (int) $line->channel_code;
            if($code >= 1000 && $code < 2000){
                $data['TeamTotal']['team1']++;
            }
            if($code >= 2000 && $code < 3000){
                $data['TeamTotal']['team2']++;
            }
            if($code >= 3000 && $code < 4000){
                $data['TeamTotal']['team3']++;
            }
        }
        
        $sqlListAll = "select channel_code from mega_member_channel where $commonSql";
        $ListAllToday = DB::select($sqlListAll.$today);
        foreach ($ListAllToday as $line){
            $code = (int) $line->channel_code;
            if($code >= 1000 && $code < 2000){
                $data['TeamToday']['team1']++;
            }
            if($code >= 2000 && $code < 3000){
                $data['TeamToday']['team2']++;
            }
            if($code >= 3000 && $code < 4000){
                $data['TeamToday']['team3']++;
            }
        }
        $this->output($data);
    }
}